﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VideoLibrary;
using DevComponents.DotNetBar.Controls;
using System.Diagnostics;

namespace MyVideo
{
    public partial class BulidRingSound : UserControl
    {
        public BulidRingSound()
        {
            InitializeComponent();
        }
        private string startTime;
        private string endTime;
        private Media media;
        private string fileName;

        private void buttonX2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.Filter = "MP3文件|*.mp3|所有文件|*.*";
            openFileDialog.RestoreDirectory = false;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                fileName = openFileDialog.FileName;
                media = new Media(fileName);
                media.PlayMedia();
                SetSlider(slider1, 100);
                string length = media.GetMusicLengthString();
                SetSlider(slider1, (int)media.GetTimeSpan(length).TotalSeconds);
                timer1.Enabled = true;
            }
        }

        private void SetSlider(Slider slider, int sum)
        {
            slider.Maximum = sum;
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            startTime = media.GetMusicPosString();
            TimeSpan timeSpan = media.GetTimeSpan(startTime);
            integerInput1.Text = timeSpan.Minutes.ToString();
            doubleInput2.Text = timeSpan.Seconds.ToString() + "." + timeSpan.Milliseconds.ToString();
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            endTime = media.GetMusicPosString();
            TimeSpan timeSpan = media.GetTimeSpan(endTime);
            integerInput2.Text = timeSpan.Minutes.ToString();
            doubleInput4.Text = timeSpan.Seconds.ToString() + "." + timeSpan.Milliseconds.ToString();
        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
            TimeSpan timeSpan = media.GetTimeSpan(startTime);
            slider1.Value = (int)timeSpan.TotalSeconds;
            media.PlayMedia(timeSpan.TotalMilliseconds, media.GetTimeSpan(endTime).TotalMilliseconds);
        }

        private void buttonX6_Click(object sender, EventArgs e)
        {
            string savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\1.mp3";
            string startTime = string.Format("0:{0}:{1}", 0, 10).Trim();
            string endTime = string.Format("0:{0}:{1}", 0, 20).Trim();
            Process pro = new Process();
            pro.StartInfo.FileName = "ffmpeg.exe";
            pro.StartInfo.Arguments = "-y -i \"" + fileName + "\" -ss " + startTime + " -t " + endTime + " -acodec copy \"" + savePath + "\"";
            //关闭Shell的使用
            pro.StartInfo.UseShellExecute = false;
            //重定向标准输入
            pro.StartInfo.RedirectStandardInput = true;
            //重定向标准输出
            pro.StartInfo.RedirectStandardOutput = true;
            //重定向错误输出
            pro.StartInfo.RedirectStandardError = true;
            //设置不显示窗口
            pro.StartInfo.CreateNoWindow = true;
            pro.Start();
            //不过要记得加上Exit要不然下一行程式执行的时候会当机
            pro.StandardInput.WriteLine("exit");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (media.GetMediaMode() == "playing")
            {
                slider1.Value++;
                labelX5.Text = media.GetMusicPosString();
            }
        }

        private void BulidRingSound_Load(object sender, EventArgs e)
        {
        }

        protected override void OnResize(EventArgs e)
        {
            panelEx1.Location = new Point((this.Width - panelEx1.Width) / 2, (this.Height - panelEx1.Height - 80) / 2);
            base.OnResize(e);
        }
    }
}
